package com.xywl.escort.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.xywl.escort.R;

/**
 * Created by 32422 on 2017/10/14.
 */

public class CustomDialog3 extends Dialog {
    public CustomDialog3(Context context) {
        super(context);
    }

    public CustomDialog3(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {

        private Context context;

        private OnClickListener receiveButtonListener, closeButtonListener, defaultListener = new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                System.out.println("i:" + i);
                dialogInterface.dismiss();
            }
        };

        public Builder(Context context) {
            this.context = context;
        }

        public CustomDialog3.Builder setReceiveButton(OnClickListener listener) {
            this.receiveButtonListener = listener == null ? defaultListener
                    : listener;
            return this;
        }

        public CustomDialog3.Builder setCloseButton(OnClickListener listener) {
            this.closeButtonListener = listener == null ? defaultListener
                    : listener;
            return this;
        }

        public CustomDialog3 create() {
            final CustomDialog3 dialog = new CustomDialog3(context,
                    R.style.CustomDialogStyle);
            LayoutInflater inflater = LayoutInflater.from(context);
            View contentView = inflater.inflate(R.layout.custom_dialog3, null);
            dialog.addContentView(contentView, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            ImageView imageWelfare = contentView.findViewById(R.id.image_welfare);
            ImageView imageClose = contentView.findViewById(R.id.image_close);

            imageWelfare.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    receiveButtonListener.onClick(dialog,
                            DialogInterface.BUTTON_POSITIVE);
                }
            });
            imageClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    closeButtonListener.onClick(dialog,
                            DialogInterface.BUTTON_NEGATIVE);
                }
            });
            dialog.setContentView(contentView);
            return dialog;
        }
    }

    @Override
    public void show() {
        super.show();
        Window window = getWindow();
        if (null != window) {
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
            window.getDecorView().setPadding(0, 0, 0, 0);
            window.setAttributes(layoutParams);
        }
    }
}
